<template>
  <div>
    <van-nav-bar title="不凡乐购" left-text="返回" left-arrow @click-left="onClickLeft" class="navbar" />
    <van-address-list
      :list="list"
      v-model="chosenAddressId"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="choose"
    />
  </div>
</template>

<script>
import { getListAction, saveAction } from "@/api/home";
export default {
  data() {
    return {
      list: [],
      chosenAddressId: ""
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    onAdd() {
      saveAction().then(res => {
        console.log(res);
      });
      this.$router.push("/cart/addpoint");
    },
    onEdit(item, index) {
      console.log(item, index);
      saveAction({
        id: item.id
      }).then(res => {
        console.log(res);
      });
      this.$router.push({ path: "/cart/edit", query: { id: item.id } });
    },
    choose(item, index) {
      console.log(item, index);
      this.$router.push({ path: "/cart/pay", query: { id: item.id } });
    }
  },
  created() {
    getListAction({
      openId: 17516331686
    }).then(res => {
      console.log(res);
      this.list = res.data;
      for (var i = 0; i < res.data.length; i++) {
        res.data[i].tel = res.data[i].mobile;
      }
      var moren = this.list.find(el => el.is_default == 1);
      // console.log(moren);
      this.chosenAddressId = moren.id;
    });
  }
};
</script>

<style lang="scss" scoped>
</style>